package org.example.volunteerend.mapper;

import org.apache.ibatis.annotations.*;
import org.example.volunteerend.entity.User;

@Mapper
public interface UserMapper {
    
    @Insert("INSERT INTO users (username, password, real_name, email, phone, age, gender, address, admin_invitation_code) " +
            "VALUES (#{username}, MD5(#{password}), #{realName}, #{email}, #{phone}, #{age}, #{gender}, #{address}, #{adminInvitationCode})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    void insert(User user);

    @Select("SELECT * FROM users WHERE username = #{username}")
    User findByUsername(String username);

    @Select("SELECT * FROM users WHERE user_id = #{userId}")
    User findById(Integer userId);

    @Update("UPDATE users SET real_name = #{realName}, email = #{email}, phone = #{phone}, " +
            "age = #{age}, gender = #{gender}, address = #{address} " +
            "WHERE user_id = #{userId}")
    void update(User user);
    
    @Update("UPDATE users SET avatar_url = #{avatarUrl} WHERE user_id = #{userId}")
    void updateAvatar(@Param("userId") Integer userId, @Param("avatarUrl") String avatarUrl);
} 